<template>
  <base-breadcrumb :title="$route.meta.title">
    <!-- 质量验收记录 -->
    <div id="base-breadcrumb" style="position: relative; overflow: hidden">
      <!-- 搜索 -->
      <searchData :searchData="searchArr" @search="search" :clickTabHandleReset="typeCode"></searchData>
      <!-- 表格 -->
      <a-card :bordered="false">
        <!-- <title-name :title="$route.meta.title"></title-name> -->
        <!-- 验收类型 -->
        <a-spin :spinning="spinning">
          <a-tabs :activeKey="checkAcceptanceTypeId" @change="handleSwitch">
            <a-tab-pane :tab="item.name" v-for="item in typeList" :key="item.id"></a-tab-pane>
          </a-tabs>
        </a-spin>
        <!-- 统计汇总 -->
        <!-- 日常验收 -->
        <AuditSummary
          title="需验收"
          :form="{
            checkAcceptanceTypeId: checkAcceptanceTypeId,
            ...queryParam
          }"
          :loadCount="loadCount"
          :api="api"
          :countList="countList1"
          v-if="typeCode === '100'"
        />
        <!-- 分部分项/竣工验收 -->
        <AuditSummary
          title="验收"
          :form="{
            checkAcceptanceTypeId: checkAcceptanceTypeId,
            ...queryParam
          }"
          :loadCount="loadCount"
          :api="api"
          :countList="countList2"
          v-else-if="typeCode === '1' || typeCode === '4'"
        />
        <!-- 单位工程/阶段验收/专项验收 -->
        <AuditSummary
          title=" "
          :form="{
            checkAcceptanceTypeId: checkAcceptanceTypeId,
            ...queryParam
          }"
          :loadCount="loadCount"
          :api="api"
          :countList="countList3"
          v-else
        />
        <a-button
          v-if="typeCode === '1' || typeCode === '4'"
          v-btnPermission="'qm_007_adda_01'" 
          class="mb12" 
          icon="plus" 
          type="primary" 
          @click="handleOpenDetail(2)"
        >
          添加验收
        </a-button>
        <!-- 表格 -->
        <list-table
          ref="table"
          tableLayout="fixed"
          size="default"
          rowKey="id"
          :columns="columns"
          :request="loadData"
          :alert="true"
          show-size-changer
          show-quick-jumper
          showPagination="auto"
        >
          <span slot="action" slot-scope="text, record">
            <MoreButton>
              <a v-btnPermission="'qm_007_loo_06'" v-if="typeCode === '100'" @click="handleOpenDetail(0, record)"
                >查看</a
              >
              <template v-else-if="typeCode === '1' || typeCode === '4'">
                <a
                  v-btnPermission="'qm_007_loo_06'"
                  @click="handleOpenDetail(0, record)"
                  >查看</a>
                <a v-if="record.status == '5'"  v-btnPermission="'qm_007_radd_04'" @click="handleOpenDetail(1, record)">重新验收</a>
              </template>
              <!-- status 0 待验收 1 预验收通过 2 预验收未通过 3 已申请，审批中 4 验收通过 5 验收未通过 -->
              <template v-else>
                <a
                  v-btnPermission="'qm_007_loo_06'"
                  @click="handleOpenDetail(0, record)"
                  >查看</a>
                  <a v-btnPermission="'qm_007_rep_03'" v-if="record.status == '1'" @click="handleOpenDetail(1, record)">补充</a>
                  <a v-btnPermission="'qm_007_appa_05'" v-if="record.status == '2'" @click="handleOpenApplication(record)">提交验收申请</a>
              </template>
            </MoreButton>
          </span>
          <!-- 状态 -->
          <template slot="statusCode" slot-scope="text, record">
            <status-display :statusCode="record.statusCode" :statusMap="statusMap"></status-display>
          </template>
        </list-table>
      </a-card>
      <!-- 验收记录 -->
      <dialog-edit
        :visible.sync="visible"
        :api="api"
        :args="form"
        headerTitle="验收记录"
        :columns="historyColumns"
        pathName="/quality/receivanceManagement/receivanceRecord/receivanceRecordEdit"
      ></dialog-edit>
    </div>
  </base-breadcrumb>
</template>

<script>
import { acceptanceRecord, typesLabel, acceptanceDailyRecord } from '@/api/quality/quality' // api
import OrgTreeSelect from '@/components/OrgTreeSelect'
import SelectSubcontractor from '@/components/Select/SelectSubcontractor' // 分包商
import StatusDisplay from '@/components/StatusDisplay'
import dialogEdit from '@/views/quality/components/VisionHistory' // 验收记录
import AuditSummary from '@/views/quality/components/AuditSummary' // 统计汇总
import SelectProjectStatus from '@/views/quality/components/SelectProjectStatus' // 项目状态

const columns100 = [
  {
    title: '分公司',
    ellipsis: true,
    dataIndex: 'branchCompanyName'
  },
  {
    title: '项目',
    ellipsis: true,
    dataIndex: 'projectName'
  },
  {
    title: '分部分项',
    ellipsis: true,
    dataIndex: 'subItemName'
  },
  {
    title: '验收区域',
    ellipsis: true,
    dataIndex: 'respAreaName'
  },
  {
    title: '计划验收时间',
    dataIndex: 'acceptanceDatePlan'
  },
  {
    title: '实际验收时间',
    dataIndex: 'acceptanceDate'
  },
  {
    title: '验收申请人',
    ellipsis: true,
    dataIndex: 'creatorName'
  },
  {
    title: '验收人',
    ellipsis: true,
    dataIndex: 'acceptorName'
  },
  {
    title: '状态',
    dataIndex: 'statusName',
    align: 'center',
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '250px',
    fixed: 'right',
    scopedSlots: { customRender: 'action' }
  }
]
const columns1 = [
  {
    title: '分公司',
    ellipsis: true,
    dataIndex: 'branchComName'
  },
  {
    title: '项目',
    ellipsis: true,
    dataIndex: 'projectName'
  },
  {
    title: '验收内容',
    ellipsis: true,
    dataIndex: 'unitEngineering'
  },
  {
    title: '分部分项',
    ellipsis: true,
    dataIndex: 'parentTypeName'
  },
  {
    title: 'WBS项',
    ellipsis: true,
    dataIndex: 'wbsName'
  },
  {
    title: '计划验收时间',
    dataIndex: 'acceptanceDatePlan'
  },
  {
    title: '实际验收时间',
    dataIndex: 'acceptanceDate'
  },
  {
    title: '验收负责人',
    ellipsis: true,
    dataIndex: 'acceptorName'
  },
  {
    title: '验收人',
    ellipsis: true,
    dataIndex: 'acceptorNames'
  },
  {
    title: '状态',
    dataIndex: 'statusName',
    align: 'center',
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '250px',
    fixed: 'right',
    scopedSlots: { customRender: 'action' }
  }
]
const columns0 = [
  {
    title: '分公司',
    ellipsis: true,
    dataIndex: 'branchComName'
  },
  {
    title: '项目',
    ellipsis: true,
    dataIndex: 'projectName'
  },
  {
    title: '验收内容',
    ellipsis: true,
    dataIndex: 'unitEngineering'
  },
  {
    title: 'WBS项',
    ellipsis: true,
    dataIndex: 'wbsName'
  },
  {
    title: '计划验收时间',
    dataIndex: 'acceptanceDatePlan'
  },
  {
    title: '实际验收时间',
    dataIndex: 'acceptanceDate'
  },
  {
    title: '验收负责人',
    ellipsis: true,
    dataIndex: 'acceptorName'
  },
  {
    title: '验收人',
    ellipsis: true,
    dataIndex: 'acceptorNames'
  },
  {
    title: '状态',
    dataIndex: 'statusName',
    align: 'center',
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '250px',
    fixed: 'right',
    scopedSlots: { customRender: 'action' }
  }
]
const columns4 = [
  {
    title: '分公司',
    ellipsis: true,
    dataIndex: 'branchComName'
  },
  {
    title: '项目',
    ellipsis: true,
    dataIndex: 'projectName'
  },
  {
    title: '验收内容',
    ellipsis: true,
    dataIndex: 'unitEngineering'
  },
  {
    title: '计划验收时间',
    dataIndex: 'acceptanceDatePlan'
  },
  {
    title: '实际验收时间',
    dataIndex: 'acceptanceDate'
  },
  {
    title: '验收负责人',
    ellipsis: true,
    dataIndex: 'acceptorName'
  },
  {
    title: '验收人',
    ellipsis: true,
    dataIndex: 'acceptorNames'
  },
  {
    title: '状态',
    dataIndex: 'statusName',
    align: 'center',
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '250px',
    fixed: 'right',
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  name: 'a' + Date.now(),
  components: {
    StatusDisplay,
    dialogEdit,
    AuditSummary
  },
  computed: {
    // 表头
    columns() {
      if (this.typeCode === '100') {
        return columns100
      } else if (this.typeCode === '1') {
        return columns1
      } else if (this.typeCode === '4') {
        return columns4
      } else {
        return columns0
      }
    },
    // api
    api() {
      return this.typeCode === '100' ? acceptanceDailyRecord : acceptanceRecord
    },
    // 搜索表单配置项
    searchArr() {
      let statusCodeArr = {}
      if (this.typeCode === '100') {
        statusCodeArr = {
              name: '验收状态',
              type: 'select',
              data: [
                {
                  value: '1',
                  label: '待验收'
                },
                {
                  value: '5',
                  label: '验收中'
                },
                {
                  value: '6',
                  label: '未完成验收'
                },
                {
                  value: '2',
                  label: '验收通过'
                },
                {
                  value: '3',
                  label: '验收未通过'
                }
              ],
              value: 'statusCode'
            }
      } else if (this.typeCode === '1' || this.typeCode === '4') {
        statusCodeArr = {
          name: '验收状态',
          type: 'select',
          data: [
            {
              value: '4',
              label: '验收通过'
            },
            {
              value: '5',
              label: '验收未通过'
            }
          ],
          value: 'statusCode'
        }
      } else {
        statusCodeArr =  {
          name: '验收状态',
          type: 'select',
          data: [
            {
              value: '0',
              label: '待验收'
            },
            {
              value: '1',
              label: '预验收通过'
            },
            {
              value: '2',
              label: '预验收未通过'
            },
            {
              value: '3',
              label: '已申请，审批中'
            },
            {
              value: '4',
              label: '验收通过'
            },
            {
              value: '5',
              label: '验收未通过'
            }
          ],
          value: 'statusCode'
        }
      }
      const list = [
        // 搜索框
        {
          name: '所属单位',
          valueKey: 'searchQuery',
          type: OrgTreeSelect,
        },
        {
          name: '项目状态',
          type: SelectProjectStatus,
          valueKey: 'projectStatus'
        },
        {
          name: '分部分项',
          type: 'input',
          value: 'subItemName'
        },
        {
          name: '分包单位',
          type: SelectSubcontractor,
          valueKey: 'workUtilsId'
        },
        statusCodeArr,
        {
          name: '填报人',
          type: 'input',
          value: 'filler'
        },
        {
          name: '实际验收时间',
          type: 'dateRange',
          value: 'createTime',
          attr: {
            rangeFormat: ['startDate', 'endDate']
          }
        },
        {
          name: '验收内容',
          type: 'input',
          value: 'unitEngineering'
        }
      ]
      return list
    }
  },
  data() {
    return {
      spinning: false, // tabs加载控件
      typeList: [], // 验收类型
      checkAcceptanceTypeId: '0', // 验收类型
      typeCode: '100', // 验收类型
      queryParam: {}, // 查询参数
      visible: false, // 弹窗显隐控件
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, {
          checkAcceptanceTypeId: this.checkAcceptanceTypeId,
          ...this.queryParam
        })
        // 日常验收
        if (this.typeCode === '100') {
          return acceptanceDailyRecord.getListByQuery(requestParameters).then((res) => {
            return res
          })
          // 分部分项和验收内容验收
        } else {
          return acceptanceRecord.postList(requestParameters).then((res) => {
            return res
          })
        }
      },
      loadCount: 0, // 汇总加载控件
      historyColumns: [
        {
          title: '验收时间',
          dataIndex: 'acceptanceDate'
        },
        {
          title: '验收结果',
          dataIndex: 'acceptanceResult'
        },
        {
          title: '操作',
          dataIndex: 'actions',
          width: '100px',
          scopedSlots: { customRender: 'actions' }
        }
      ], // 验收记录表头
      statusMap: {
        0: {
          label: '待验收',
          color: '#FE5500'
        },
        1: {
          label: '已申请，未验收',
          color: '#FE5500'
        },
        2: {
          label: '已验收',
          color: '#00A854'
        },
        3: {
          label: '验收未通过',
          color: '#FE5500'
        },
        4: {
          label: '不可填报',
          color: '#FE5500'
        },
        5: {
          label: '已申请，审批中',
          color: '#00A854'
        }
      }, // 验收状态枚举
      form: {}, // 弹窗表单
      countList1: [
        {
          title: '总数：',
          value: 'total'
        },
        {
          title: '待验收：',
          value: 'not'
        },
        {
          title: '已验收：',
          value: 'accepted'
        },
        {
          title: '验收已通过：',
          value: 'acceptedPass'
        },
        {
          title: '验收不通过：',
          value: 'noPass'
        },
        {
          title: '验收通过率：',
          value: 'acceptedPassRate',
          needUnit: false
        }
      ], // 汇总列表
      countList2: [
        {
          title: '总数：',
          value: 'total'
        },
        {
          title: '验收通过：',
          value: 'passCount'
        },
        {
          title: '验收未通过：',
          value: 'notPassCount'
        },
        {
          title: '验收通过率：',
          value: 'rate',
          needUnit: false
        }
      ],
      countList3: [
      {
          title: '总数：',
          value: 'total'
        },
        {
          title: '待验收：',
          value: 'treatCount'
        },
        {
          title: '预验收通过：',
          value: 'advancePassCount'
        },
        {
          title: '预验收未通过：',
          value: 'advanceNotPassCount'
        },
        {
          title: '已申请，审批中：',
          value: 'submitCount'
        },
        {
          title: '验收未通过：',
          value: 'notPassCount'
        },
        {
          title: '验收通过：',
          value: 'passCount'
        },
        {
          title: '验收通过率：',
          value: 'rate',
          needUnit: false
        }
      ]
    }
  },
  created() {
    this.spinning = true
    typesLabel
      .getList({ type: '23' })
      .then((res) => {
        this.spinning = false
        if (res.code === 200 && res.data?.length) {
          this.typeList = [
            {
              id: '0',
              name: '日常验收',
              code: '100'
            },
            ...res.data
          ]
        }
      })
      .catch(() => {
        this.spinning = false
      })
  },
  methods: {
    /**
     * @description: 切换验收类型
     * @param {*} e：切换id
     * @return {*}
     */
    handleSwitch(e) {
      this.checkAcceptanceTypeId = e
      const list = this.typeList.filter(item => item.id === e)
      if (list && list.length > 0) {
        this.typeCode = list[0].code
      }
    },
    /**
     * 搜索
     */
    search(value,flag) {
      value && (this.queryParam = value)
      if (this.$refs.table) {
        this.$refs.table.refresh(true)
        this.loadCount++
      }
    },
    // 提交验收申请 
    handleOpenApplication(record) {
      let query = {}
      query.editModel = 3 // 3 提交验收申请
      query.projectId = record.projectId
      query.branchCompanyId = record.branchComId
      query.detailsId = record.detailId
      query.detailsName = record.unitEngineering
      query.checkAcceptanceTypeId = record.checkAcceptanceTypeId
      query.checkAcceptanceTypeName = record.checkAcceptanceTypeName
      query.acceptorName = record.acceptorName
      this.$router.push({
        name: '/quality/preAcceptanceManagement/preAcceptanceApplication/preAcceptanceApplicationEdit',
        query
      })
    },
    // 0 查看 1 修改 2 新增
    handleOpenDetail(editModel, record) {
      const typeCode = this.typeCode
      let name = ''
      let query = {
        editModel
      }
      if (record?.id) {
        query.id = record.id
      }
      if (typeCode === '100') {
        // 日常验收
        name = '/quality/receivanceManagement/receivanceRecord/acceptanceDailyRecordEdit'
      } else if (typeCode === '1') {
        // 分部分项
        name = '/quality/receivanceManagement/receivanceRecord/acceptanceSubItemEdit'
      } else if (typeCode === '4') {
        // 竣工验收
        name = '/quality/receivanceManagement/receivanceRecord/acceptanceCompletionEdit'
      } else {
        // 单位工程/阶段验收/专项验收
        name = '/quality/receivanceManagement/receivanceRecord/acceptanceSpecialEdit'
      }
      this.$router.push({
        name,
        query
      })
    },
  }
}
</script>
